import java.lang.reflect.Method;

/**
 * describe:
 *
 * @author helloworldyu
 * @data 2018/1/3
 */
public class Event {
	//要执行方法的对象
	private Object object;
	//要执行的方法名称
	private String methodName;
	//要执行方法的参数
	private Object[] params;
	//要执行方法的参数类型
	private Class[] paramTypes;

	public Event() {
	}

	public Event(Object object, String methodName, Object... params) {
		this.object = object;
		this.methodName = methodName;
		this.params = params;
		this.paramTypes = paramTypes;
		getParamTypes(params);
	}


	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public Object[] getParams() {
		return params;
	}

	public void setParams(Object[] params) {
		this.params = params;
	}

	public Class[] getParamTypes() {
		return paramTypes;
	}

	public void setParamTypes(Class[] paramTypes) {
		this.paramTypes = paramTypes;
	}

	/**
	 * 执行指定对象的该方法
	 * @throws Exception
	 */
	public void invoke() throws Exception {
		Method method = object.getClass().getMethod(this.methodName, this.paramTypes);
		if( null == method ){
			throw new NullPointerException("找不到指定的方法"+object.getClass().toString()+paramTypes.toString());
		}
		method.invoke(this.object,this.params);
	}

	/**
	 * 获取每个参数的类型
	 * @param params
	 */
	private void getParamTypes(Object... params){
		this.paramTypes = new Class[params.length];
		for ( int i=0; i<params.length; ++i ){
			this.paramTypes[i]=params[i].getClass();
		}
	}


}